package global

import (
	"errors"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"github.com/spf13/viper"
)

var (
	GDB *gorm.DB
)

// 初始化mysql连接
func InitMysql() {
	var err error
	dns := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true&loc=Local",
		viper.GetString("storage.user"),
		viper.GetString("storage.password"),
		viper.GetString("storage.host"),
		viper.GetString("storage.port"),
		viper.GetString("storage.dbname"),
		viper.GetString("storage.charset"),
	)

	// 初始化gorm
	GDB, err = gorm.Open("mysql", dns)
	if err != nil {
		panic(errors.New("gorm init mysql err"))
	}

	// 开启日志
	GDB.LogMode(true)

}
